package com.travel.manager.util;

import com.travel.manager.service.IndexAdvService;
import common.pojo.IndexAdv;
import common.util.Constants;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class AdvJob {

    @Autowired
    private IndexAdvService indexAdvService;

    public void execute() {
        //下架过期的广告位
        IndexAdv criteria = new IndexAdv();
        criteria.setStatus(Constants.ADV_STATUS_SHOW);
        List<IndexAdv> advList = indexAdvService.getIndexAdvForUpdate(criteria);//显示中的广告位
        for (IndexAdv indexAdv : advList) {
            if (indexAdv.getEndDate().before(new Date())) {
                indexAdv.setStatus(Constants.ADV_STATUS_TIMEOUT);
                indexAdvService.updateIndexAdvStatus(indexAdv);
            }
        }
        //上架新的广告位
        criteria.setStatus(Constants.ADV_STATUS_CREATED);
        advList = indexAdvService.getIndexAdvForUpdate(criteria);//新添加的广告位
        for (IndexAdv indexAdv : advList) {
            if (indexAdv.getStartDate().before(new Date())) {
                indexAdv.setStatus(Constants.ADV_STATUS_SHOW);
                indexAdvService.updateIndexAdvStatus(indexAdv);
                indexAdvService.exportDetailPage(indexAdv.getHotel().getId());
            }
        }
        indexAdvService.exportIndexPage();
    }
}
